import 'package:flutter/material.dart';
import 'package:razer_one/app/main/page/main_status_container.dart';
import 'package:razer_one/app/main/view/main_btn.dart';
import 'package:razer_one/app/main/view/main_load_more_view.dart';
import 'package:razer_one/app/main/view/main_slide_cell.dart';
import 'package:razer_one/app/main/view/main_text.dart';
import 'package:razer_one/app/modules/home/home/home_controller.dart';
import 'package:get/get.dart';

class HomePage extends GetView<HomeController> {
  const HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GetBuilder<HomeController>(builder: (_) {
      return Scaffold(
        backgroundColor: Colors.white,
        appBar: _buildAppBar(),
        body: _buildContentView(),
      );
    });
  }

  _buildAppBar() {
    return AppBar(
      title: const Text("首页"),
      leading: IconButton(
        onPressed: controller.headBtnClick,
        icon: Image.asset(
          "lib/app/resource/images/commond/commond_head.png",
          width: 35,
          height: 35,
        ),
      ),
      actions: [
        IconButton(
            onPressed: controller.addBtnClick,
            icon: const Icon(
              Icons.add,
              size: 35,
            ))
      ],
    );
  }

  _buildContentView() {
    return MainStatusContainer(
        statusModel: controller.statusModel,
        netErrorHandle: controller.netErrorHandle,
        downRefreshHandle: controller.downRefreshHandle,
        upRefreshHandle: controller.upRefreshHandle,
        emptyText: "暂时没有任务",
        emptyBtnHandle: controller.emptyBtnHandle,
        child: GestureDetector(
          onHorizontalDragDown: (detail) {
            controller.onHorizontalDragDown();
          },
          child: ListView.builder(
            itemCount: controller.dataArr.length + 1,
            itemBuilder: ((context, index) {
              if (index == controller.dataArr.length) {
                return MainLoadMoreView(
                    netErrorHandle: controller.upRefreshHandle,
                    statusModel: controller.statusModel);
              }
              return GestureDetector(
                onTap: () {
                  controller.didSelectItemAtIndex(index);
                },
                child: Column(
                  children: [
                    MainSlideCell(
                      onSlideStarted: () {
                        controller.onSlideStarted(index);
                      },
                      onSlideCanceled: () {
                        controller.onSlideCanceled(index);
                      },
                      child: Container(
                        color: Colors.red,
                        padding: const EdgeInsets.all(15),
                        child: GestureDetector(
                          behavior: HitTestBehavior.opaque,
                          child: Row(
                              mainAxisAlignment: MainAxisAlignment.spaceBetween,
                              children: [
                                MainText(controller.dataArr[index].title),
                                Image.asset(
                                    "lib/app/resource/images/commond/commond_arrow.png")
                              ]),
                        ),
                      ),
                      buttons: [
                        MainTextBtn("删除",
                            textNormalColor: Colors.white,
                            backgroundNormalColor: Colors.red,
                            btnW: 80,
                            btnH: double.infinity, handle: () {
                          controller.deleteItemAtIndex(index);
                        }),
                      ],
                      singleButtonWidth: 80,
                      index: index,
                    ),
                    sizeLine(Get.width - 20, 0.5),
                    // ListView.builder(
                    //   itemCount: 10,
                    //   itemBuilder: (context, index) {
                    //   return Row();
                    // })
                  ],
                ),
              );
            }),
          ),
        ));
  }
}
